package com.ikproducts.manualian.client.entrypoint;

import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.ikproducts.manualian.client.GreetingService;
import com.ikproducts.manualian.client.GreetingServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Manualian implements EntryPoint {
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		
		//-------------------------------------------------------------
		// ログイン・ログアウトの判定
		//-------------------------------------------------------------
		final StringBuilder strHtml = new StringBuilder();
		greetingService.greetServer("", new AsyncCallback<Map<String,String>>(){
			public void onFailure(Throwable caught) {
			}

			public void onSuccess(Map<String,String> result) {
				if(result.get("isLoggedIn").equals("true")){
					strHtml.append("<b>" + result.get("emailAddress") + "</b>");
					strHtml.append("&nbsp;&nbsp;");
					strHtml.append("<a href=\"news/\">マイページ </a>&nbsp;&nbsp;<a href=\"" 
							+ result.get("logoutUrl")
							+ "\">ログアウト</a>");
				}else{
					strHtml.append("<a href=\"login\">ログイン</a>");
				}
				HTML html = new HTML();
				html.setHTML(strHtml.toString());
				RootPanel.get("sub_header").add(html);
			}
		});
		
	}
}
